package com.miplus.ccrm.core.service.commission;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionTempEditVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionTempListReqVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionTempReqVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionTempResVo;
import com.miplus.ccrm.core.controller.vo.strategy.RuleTaskReqVo;
import com.miplus.ccrm.core.pojo.FiCommissionTemplate;
import com.miplus.ccrm.core.service.commission.FiCommissionTemplateService;
import com.miplus.ccrm.core.mapper.FiCommissionTemplateMapper;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author wangzl
* @description 针对表【fi_commission_template】的数据库操作Service实现
* @createDate 2025-02-28 10:03:05
*/
@Service
public class FiCommissionTemplateServiceImpl extends MPJBaseServiceImpl<FiCommissionTemplateMapper, FiCommissionTemplate>
    implements FiCommissionTemplateService{
    @Override
    public IPage<CommissionTempResVo> getCommissionTemplateList(CommissionTempListReqVo para)
    {
        return selectJoinListPage( new Page<>(para.getPageNum(), para.getPageSize()),
                CommissionTempResVo.class,
                new MPJLambdaWrapper<FiCommissionTemplate>()
                        .eq(StringUtils.isNotEmpty(para.getTemplateCode()),FiCommissionTemplate::getTemplateCode,para.getTemplateCode())
                        .like(StringUtils.isNotEmpty(para.getTemplateName()),FiCommissionTemplate::getTemplateName,para.getTemplateName())
                        .eq(StringUtils.isNotEmpty(para.getDisttag()),FiCommissionTemplate::getDisttag,para.getDisttag())
                        .eq(ObjectUtils.isNotNull(para.getCommissionRule()),FiCommissionTemplate::getCommissionRule,para.getCommissionRule())
                        .ge(StringUtils.isNotEmpty(para.getStartDate()),FiCommissionTemplate::getStartdate,para.getStartDate())
                        .lt(StringUtils.isNotEmpty(para.getEndDate()),FiCommissionTemplate::getStartdate,para.getEndDate())
                        .selectAll(FiCommissionTemplate.class)
                        .selectAs("fun_getnames('SORTING.QUEUE',t.disttag)", CommissionTempResVo::getDisttagDesc)
                        .selectAs("fun_getnames('COMMISSION.RULE',t.commission_rule)", CommissionTempResVo::getCommissionRuleDesc)
        );
    }
    //RuleTaskListResVo getRuleTaskInfo(Long taskId);
    @Override
    public CmdResVo addCommissionTemplate(String usrId, FiCommissionTemplate fiCommissionTemplate)
    {
        fiCommissionTemplate.setId(null);
        fiCommissionTemplate.setCrdt(new Date());
        fiCommissionTemplate.setCrusr(usrId);
        save(fiCommissionTemplate);
        return new CmdResVo( 1, fiCommissionTemplate.getId().toString());
    }
    @Override
    public CmdResVo updateCommissionTemplate(String usrId, CommissionTempEditVo commissionTempEditVo)
    {
        if(ObjectUtils.isNull(commissionTempEditVo.getId()))
        {
            return new CmdResVo(-1, "参数错误");
        }
        FiCommissionTemplate fiCommissionTemplate = new FiCommissionTemplate();
        BeanUtils.copyProperties(commissionTempEditVo,fiCommissionTemplate);
        fiCommissionTemplate.setMddt(new Date());
        fiCommissionTemplate.setMdusr(usrId);
        updateById(fiCommissionTemplate);

        return new CmdResVo(1,"更新成功");
    }
    @Override
    public CmdResVo deleteCommissionTemplate(Integer id)
    {
        if(ObjectUtils.isNull(id))
        {
            return new CmdResVo(-1, "参数错误");
        }
        removeById(id);
        return new CmdResVo(1,"删除成功");
    }
    @Override
    public FiCommissionTemplate getAvailTemplate(String disttag, String day)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String compareDay = df.format(new Date());
        if( StringUtils.isNotEmpty(day))
            compareDay = day;
        List<FiCommissionTemplate> fiCommissionTemplates = selectJoinList(
                FiCommissionTemplate.class,
                new MPJLambdaWrapper<FiCommissionTemplate>()
                        .eq(FiCommissionTemplate::getDisttag, disttag)
                        .le(FiCommissionTemplate::getStartdate,compareDay)
                        .orderByDesc(FiCommissionTemplate::getStartdate)
                        .selectAll(FiCommissionTemplate.class)

        );
        if(fiCommissionTemplates == null || fiCommissionTemplates.size() == 0 )
            return null;
        return fiCommissionTemplates.get(0);
    }
}




